package com.elesyser.alarm;

import com.elesyser.R;
import com.elesyser.main.CurriculumActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.RecoverySystem;
import android.os.Vibrator;
import android.util.Log;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {

	Vibrator vibrator;

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Bundle bundle = intent.getExtras();
		String type = bundle.getString("type");
		String message = bundle.getString("message");
		if (type.equals(AlarmRegister.ADD)) {
			NotificationManager notificationManager = (NotificationManager) context
					.getSystemService(Context.NOTIFICATION_SERVICE);
			try {
				Notification notification = new Notification(R.drawable.icon,
						message, System.currentTimeMillis());

				PendingIntent pIntent = PendingIntent.getActivity(context, 0,
						new Intent(context, CurriculumActivity.class), 0);
				notification.setLatestEventInfo(context, "Hi",
						message, pIntent);
				notificationManager.notify(1, notification);

				AudioManager audioManager = (AudioManager) context
						.getSystemService(Context.AUDIO_SERVICE);

				audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				Log.d("notify", e.getMessage());
			}

			Toast.makeText(context, message,
					Toast.LENGTH_LONG).show();
		} else if (type.equals( AlarmRegister.CANCEL)) {
			Recover(context);
			Toast.makeText(context,"下课啦",Toast.LENGTH_LONG).show();
		}
	}

	private void Recover(Context context) {
		AudioManager audioManager = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
		audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		notificationManager.cancel(1);
	}

}
